(function($) {

	var event_widget = {
    };

	$.fn.event_widget = function() {

		var options = {
			src_element: $(this),
		    new_form: false,
			edit_form: false,
			editable: false,
		 	eventdata: dasding.prototypes.event_prototype
		}

	    if($.urlParam('new') && $.urlParam('new') == "true") {
	      options.new_form = true;
	      options.editable = true;
	      display_event(options);
	    } else if($.urlParam('edit') && $.urlParam('edit') == "true") {
	      $.getJSON('resources/dummy/Event', function(data) {
	        options.eventdata = data;
	        options.edit_form = true;
	        options.editable = true;
	        display_event(options);
	      });
	    } else {
	      $.getJSON('resources/dummy/Event', function(data) {
	        options.eventdata = data;
	        display_event(options);
	      });
	    }
	}

	function display_event(options) {
		var data = options.eventdata;
		var dd_event_details = $("<div>").addClass("box").addClass("no-left_margin").attr({"id": "dd_event_details"});
		if(options.editable) dd_event_details.addClass("editable");
		var form = $("<form>").addClass("event_form").attr({"id": data.id, "name": data.id});
		form.append($("<h3>").text(data.name));
		form.append($("<input>").attr({"type": "hidden", "id": "eventdata_id", "value": data.id}));

		var button_row = $("<div class='row-fluid'>");
		button_row.append($("<input>").attr({"type": "button", 
			"class": "dd_save_button edit_enabled", "value": "speichern"}));
		button_row.append($("<input>").attr({"type": "button", 
			"class": "dd_edit_button edit_disabled", "value": "editieren"}));
		form.append(button_row);

		var temp_row = $("<div class='row-fluid'>");
		temp_row.append($("<label>").addClass("dd_label span4").text("Name:"));
		temp_row.append($("<input>").addClass("span8 edit_enabled").attr({"id": "eventdata_name", 
			"value": data.name}));
		temp_row.append($("<div>").addClass("span8 edit_disabled").text(data.name));
		form.append(temp_row);

		var temp_row = $("<div class='row-fluid'>");
		temp_row.append($("<label>").addClass("dd_label span4").text("Beschreibung:"));
		temp_row.append($("<input>").addClass("span8 edit_enabled").attr({"id": "eventdata_description", 
			"value": data.description}));
		temp_row.append($("<div>").addClass("span8 edit_disabled").text(data.description));
		form.append(temp_row);

		var temp_row = $("<div class='row-fluid'>");
		temp_row.append($("<label>").addClass("dd_label span4").text("Termin:"));
		temp_row.append($("<input>").addClass("span8 edit_enabled").attr({"id": "eventdata_time", 
			"value": data.time}));
		temp_row.append($("<div>").addClass("span8 edit_disabled").text(data.time));
		form.append(temp_row);

		var temp_row = $("<div class='row-fluid'>");
		temp_row.append($("<label>").addClass("dd_label span4").text("Location:"));
		var location_select = $("<select>").addClass("span8 edit_enabled").attr({"id": "eventdata_location"});
		location_select.append($("<option>").text(data.location.name + ": " + data.location.zip + " " + data.location.city));
		temp_row.append(location_select);
		temp_row.append($("<div>").addClass("span8 edit_disabled").text(data.location.name + ": " + data.location.zip + " " + data.location.city));
		form.append(temp_row);

		dd_event_details.append(form);

		//aktionen

		var actions_container = $("<div class='row-fluid'>");
		actions_container.append($("<h4>").text("Aktionen"));
		jQuery.each(data.actions.action, function(key, action) {
	    	var temp_form = $("<form>").addClass("action_form").attr({"id": action.id, "name": action.id});
	    	actions_container.append(temp_form);
	    	var action_container = $("<div>").addClass("span12 box no-left_margin dd_" + action.status);

	    	var temp_row = $("<div class='row-fluid'>");
	    	temp_row.append($("<label>").addClass("dd_label span4").text("Aktion"));
	    	temp_row.append($("<input>").addClass("span8 edit_enabled action_name").attr({"value": action.name}));
	    	temp_row.append($("<div>").addClass("span8 edit_disabled").text(action.name));
	    	action_container.append(temp_row);

	    	var temp_row = $("<div class='row-fluid'>");
	    	temp_row.append($("<label>").addClass("dd_label span4").text("Description"));
	    	temp_row.append($("<input>").addClass("span8 edit_enabled action_description").attr({"value": action.description}));
	    	temp_row.append($("<div>").addClass("span8 edit_disabled").text(action.description));
	    	action_container.append(temp_row);

	    	var temp_row = $("<div class='row-fluid'>");
	    	temp_row.append($("<label>").addClass("dd_label span4").text("Status"));
	    	temp_row.append($("<input>").addClass("span8 edit_enabled action_status").attr({"value": action.status}));
	    	temp_row.append($("<div>").addClass("span8 edit_disabled").text(action.status));
	    	action_container.append(temp_row);

	    	var temp_row = $("<div class='row-fluid'>");
	    	temp_row.append($("<label>").addClass("dd_label span4").text("Kategorie:"));
	    	var category_select = $("<select>").addClass("span8 edit_enabled action_category");
	    	category_select.append($("<option>").text(action.category.name));
	    	temp_row.append(category_select);
	    	temp_row.append($("<div>").addClass("span8 edit_disabled").text(action.category.name));
	    	temp_row.append($("<div>").text(action.category.description));
	    	action_container.append(temp_row);

	    	var temp_row = $("<div class='row-fluid'>");
	    	temp_row.append($("<label>").addClass("dd_label span4").text("Location"));
	    	var location_select = $("<select>").addClass("span8 edit_enabled action_location").attr({"id": "eventdata_location"});
	    	location_select.append($("<option>").text(action.location.name + ": " + action.location.zip + " " + action.location.city));
	    	temp_row.append(location_select);
	    	temp_row.append($("<div>").addClass("span8 edit_disabled").text(action.location.name + ": " + action.location.zip + " " + action.location.city));
	    	action_container.append(temp_row);

	    	var temp_row = $("<div class='row-fluid'>");
	    	temp_row.append($("<label>").addClass("dd_label span4").text("Kontakt"));
	    	temp_row.append($("<input>").addClass("span8 edit_enabled action_status").attr({"value": action.contact.fullname}));
	    	temp_row.append($("<div>").addClass("span8 edit_disabled").text(action.contact.fullname));
	    	action_container.append(temp_row);


	    	temp_form.append(action_container);

		});
		dd_event_details.append(actions_container);
		dd_event_details.append(button_row.clone());
		$(options.src_element).append(dd_event_details);

		$("body").delegate(".dd_save_button", "click",  function() {
			view.eventdata.name = $("#eventdata_name").val();
			view.eventdata.description = $("#eventdata_description").val();
			view.eventdata.time = $("#eventdata_time").val();
			view.eventdata.location = $("#eventdata_location").val();
			$(view.eventdata.actions.action).each(function(key, value) {
			  if($(".action_form#"+value.id).length > 0) {
			    value.name = $(".action_form#"+value.id+" .action_name").eq(0).val();
			    value.description = $(".action_form#"+value.id+" .action_description").eq(0).val();
			    value.status = $(".action_form#"+value.id+" .action_status").eq(0).val();
			    value.category = $(".action_form#"+value.id+" .action_category").eq(0).val();
			    value.contact = $(".action_form#"+value.id+" .action_contact").eq(0).val();
			    value.location = $(".action_form#"+value.id+" .action_location").eq(0).val();
			  }
			});
			var data = view.eventdata;
			$.post('ajax/test.html', view.eventdata, function(data) {
			  console.log("success");
			});
		});

		$("body").delegate(".dd_edit_button", "click",  function() {
			$("#dd_event_details").addClass("editable");
		});


  	}

})(jQuery);